/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dominio;

import DriverNoticia.*;
import java.io.*;
import java.net.*;

/**
 *
 * @author Juanma Fuentes Diaz
 */
public class DriverNoticia {

    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static StringBuffer str = new StringBuffer();
    static DiccionarioPalabras dp = new DiccionarioPalabras();
    static int i;

    private static void addFichero(DiccionarioPalabras dp) throws Exception {
        try {
            String fi = "c:\\prop\\FNCast.txt";
            File f = new File(fi);
            BufferedReader entrada = new BufferedReader(new FileReader(f));
            int q = Integer.parseInt(entrada.readLine());
            Palabra2[] x = new Palabra2[q];
            for (int i = 0; i < q; i++) {
                x[i] = new Palabra2();
            }
            String pal;
            for (int i = 0; i < q; i++) {
                pal = entrada.readLine();
                x[i].setValor(pal);
                if (!(dp.existLetra(x[i].getValor().charAt(0)))) {
                    System.out.println("Primera letra de palabra invalida");
                } else if (dp.existPalabra(x[i].getValor()) != 0) {
                    System.out.println("La palabra cuyo string es " + x[i].getValor() + " ya existe");
                } else {

                    if (dp.addPalabra(x[i])) {
                    //System.out.println("insertado " + x[i].getValor() + "\n");
                    } else {
                        System.out.println("no se ha insertado la palabra :\"" + x[i].getValor() + "\"");
                    }
                }
            }
            System.out.print("\n");
            System.out.println("Diccionario palabras no funcionales cargado");
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }

    public static void main(String[] args) throws Exception {
        dp.crearDiccionarioVacio();
        addFichero(dp);
        System.out.println("Escribe el archivo html, xml, txt (ejemplo en windows: c:\\web.html), o bien la direccion web. ");
        System.out.println();
        try {
            String fi = in.readLine();
            if (fi.substring(0, 4).compareTo("http") == 0) {
                URL page = new URL(fi);
                HttpURLConnection conn = (HttpURLConnection) page.openConnection();
                conn.connect();
                InputStreamReader inr = new InputStreamReader((InputStream) conn.getContent());
                BufferedReader buff = new BufferedReader(inr);
                String line;
                do {
                    line = buff.readLine();
                    str.append(line + " ");
                } while (line != null);
                ParserHttp not = new ParserHttp();
                System.out.println(str);
                TitTex tt = new TitTex();
                tt = not.sacaTexto(str);
                System.out.println("TITULO: " + tt.titulo + " TEXTO: " + tt.texto);
                Texto tx = new Texto(tt.titulo.concat(". " + tt.texto), dp);
                System.out.println(tx.consultarTexto());
                System.out.println(tx.consultarBiClaves());
                System.out.println(tx.consultarClaves());
            } else {
                File f = new File(fi);
                BufferedReader di = new BufferedReader(new FileReader(f));
                String linea;
                do {
                    linea = di.readLine();
                    if (linea == null) {
                        break;
                    } else {
                        str.append(linea + " ");
                    }
                } while (true);
                ParserDisc not = new ParserDisc();
                System.out.println(str);
                TitTex tt = new TitTex();
                tt = not.sacaTexto(str);
                System.out.println("TITULO: " + tt.titulo + " TEXTO: " + tt.texto);
                Texto tx = new Texto(tt.titulo.concat(". " + tt.texto), dp);
                System.out.println(tx.consultarTexto());
                System.out.println(tx.consultarBiClaves());
                System.out.println(tx.consultarClaves());
            }
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }

    }
}
